library(tidyverse)
## ── Attaching packages ──────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✔ ggplot2 3.2.1     ✔ purrr   0.3.3
## ✔ tibble  2.1.3     ✔ dplyr   0.8.5
## ✔ tidyr   1.0.2     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.5.0
## ── Conflicts ─────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(gganimate)
## No renderer backend detected. gganimate will default to writing frames to separate files
## Consider installing:
## - the `gifski` package for gif output
## - the `av` package for video output
## and restarting the R session
source('covid_log_log_diff/functions.R')
covidByState <- loadAndFormatNytimesCovidPerState()

covidByState<-covidByState %>% 
  dplyr::filter(!is.na(newCasesPerDay), 
                !is.na(cases), 
                newCasesPerDay > 0, 
                cases > 0)  %>%
  dplyr::select(-fips,-prevDate,-prevCases)

snippet<-covidByState %>% group_by(date) %>% 
  summarize(
    state = "USA",
    cases = sum(cases),
    deaths=sum(deaths),
    newCasesPerDay = sum(newCasesPerDay)
  )

covidByState = bind_rows(snippet, covidByState)

# create loess-smoothed versions of time series for each state
covidByStateSmoothed <- covidByState %>%
  filter(!(state %in% c("USA","Northern Mariana Islands","Virgin Islands","Guam"))) %>%
  group_by(state) %>%
  do(data.frame(.,
                smoothed = 10^predict(loess(log10(newCasesPerDay) ~ log10(cases), data = .), .))) %>%
  ungroup()

1 Single state, highlighted with background

covidByStateSmoothed %>%
  filter(state == "New York") %>%
  ggplot(aes(x=cases, y=smoothed)) +
  geom_line(data = covidByStateSmoothed, aes(group = state), color = "grey") +
  geom_line(aes(y = smoothed), color = "red") +
  scale_x_log10(label = comma) + 
  scale_y_log10(label = comma) +
  coord_equal() +
  theme_minimal() +
  labs(x = 'Total confirmed cases',
       y = 'New confirmed cases per day',
       title = 'Trajectory of COVID-19 cases for New York State',
       subtitle = 'Grey lines show other U.S. states')

1.1 Interactive

#d <- highlight_key(covidByStateSmoothed, ~state)

selected_state <- "New York"

background_states <- c("New York", "New Jersey", "California", "Michigan", "Louisiana", "Florida", "Massachusetts", "Illinois", "Pennsylvania", "Washington")
background_states <- setdiff(background_states, selected_state)

plot_data <- covidByStateSmoothed %>%
  filter(state %in% background_states) %>%
  ungroup() %>%
  arrange(state, date) %>%
  mutate(state = as.factor(state),
         state = reorder(state, -newCasesPerDay, last))
  
d <- plot_data %>%
  highlight_key(~state)
## This version of Shiny is designed to work with 'htmlwidgets' >= 1.5.
##     Please upgrade via install.packages('htmlwidgets').
covidByStateSmoothMostRecent <- plot_data %>%
  group_by(state) %>%
  arrange(desc(date)) %>%
  slice(1) %>%
  ungroup()

p <- covidByStateSmoothed %>%
  filter(state == selected_state) %>%
  ggplot(aes(x=cases, y=smoothed)) +
  #geom_line(data = d, aes(group = state, text = sprintf('%s on %s:\n%s total cases\n%s new cases', state, date, comma(cases), comma(newCasesPerDay))), color = "grey") +
  geom_line(data = d, aes(group = state, color = state, text = sprintf('%s on %s:\n%s total cases\n%s new cases', state, date, comma(cases), comma(newCasesPerDay))), alpha = 0.5) +
  #geom_point(data = covidByStateSmoothMostRecent, aes(x = cases, y = smoothed, group = state), size = 2, color = "grey", alpha = 0.5) +
  geom_point(data = covidByStateSmoothMostRecent, aes(x = cases, y = smoothed, group = state, color = state), size = 2, alpha = 0.5) +
  #geom_point(data = d, aes(x = last(cases), y = last(smoothed), text = sprintf('Foo')), size = 2, color = "red", alpha = 0.5) +
  geom_line(aes(y = smoothed), color = "black") +
  geom_point(aes(x = last(cases), y = last(smoothed)), size = 2, color = "black", alpha = 0.5) +
  geom_text(aes(x = last(cases), y = 1.2*last(smoothed), label = state)) +
  geom_point(aes(y = smoothed, text = sprintf('%s on %s:\n%s total cases\n%s new cases', state, date, comma(cases), comma(newCasesPerDay))), size = 0, alpha = 0) +
  scale_x_log10(label = comma) + 
  scale_y_log10(label = comma) +
  coord_equal() +
  theme_minimal() +
  labs(x = 'Total confirmed cases',
       y = 'New confirmed cases per day',
       title = 'Trajectory of COVID-19 cases for New York State',
       subtitle = 'Grey lines show other U.S. states')
## Warning: Ignoring unknown aesthetics: text

## Warning: Ignoring unknown aesthetics: text
  #scale_color_brewer(type = "qualitative", palette = "Pastel1")

fig <- ggplotly(p, tooltip = "text") #%>%
  #highlight(on = "plotly_hover", off = "plotly_deselect", color = "black" )

fig

2 All states, faceted

ggplot(covidByStateSmoothed, aes(x=cases, y=smoothed, group = state)) +
  geom_line(data = covidByStateSmoothed %>% rename(group = state),
            aes(x = cases, y = smoothed, group = group), color = "grey") +
  geom_line(aes(y = smoothed), color = "red") +
  scale_x_log10(label = comma, breaks = c(100, 1000, 100000)) + 
  scale_y_log10(label = comma) +
  coord_equal() +
  labs(x = 'Total confirmed cases',
       y = 'New confirmed cases per day',
       title = 'Trajectory of COVID-19 cases in the U.S.') +
  facet_wrap(~ state) +
  theme_minimal()

3 Single state, highlighted with background, animated

covidByStateSmoothed %>%
  filter(state == "New York") %>%
  ggplot(aes(x=cases, y=smoothed)) +
  geom_line(data = covidByStateSmoothed, aes(group = state), color = "grey") +
  geom_line(aes(y = smoothed), color = "red") +
  geom_label(aes(label = state)) +
  geom_text(aes(x = 3e4, y = 1.5, label = date)) +
  scale_x_log10(label = comma) + 
  scale_y_log10(label = comma) +
  coord_equal() +
  theme_minimal() +
  transition_reveal(date) +
  labs(x = 'Total confirmed cases',
       y = 'New confirmed cases per day',
       title = 'Trajectory of COVID-19 cases for New York State',
       subtitle = 'Grey lines show other U.S. states')
## Warning: No renderer available. Please install the gifski, av, or magick
## package to create animated output
## NULL

4 All states, animated

covidByStateSmoothed %>%
  filter(state != "USA") %>%
  ggplot(aes(x=cases, y=smoothed, group = state)) +
  geom_text(aes(label = state)) +
  geom_text(aes(x = 3e4, y = 1.5, label = date)) +
  geom_line(aes(y = smoothed)) +
  scale_x_log10(label = comma) + 
  scale_y_log10(label = comma) +
  coord_equal() +
  transition_reveal(date) +
  theme_minimal() +
  labs(x = 'Total confirmed cases',
       y = 'New confirmed cases per day',
       title = 'Trajectory of COVID-19 cases in the U.S.')
## Warning: No renderer available. Please install the gifski, av, or magick
## package to create animated output
## NULL